<template>
  <div v-loading="loading">
    <CommonPage :state="state" :flowId="infoData.flowId" @submitForm="submitForm">
      <Info style="padding: 20px" ref="form" v-model="infoData" :state="state"></Info>
    </CommonPage>

    <!--        <div v-if="'write'==state" class="card-wrap flex justify-end card-padding">-->
    <!--            <el-button :loading="loading" type="primary" @click="submitForm(0)">保存</el-button>-->
    <!--            <el-button @click="cancel">取消</el-button>-->
    <!--            <el-button :loading="loading" type="primary" @click="submitForm(1)">提交</el-button>-->
    <!--        </div>-->

  </div>
</template>

<script>

import {
  driverAdd,
  driverDetail,
  driverUpdate,
  driverCompanySelect, driverCompanyList, driverFlowAdd
} from "@/api/memberManage/driverList";
import {supplierAdd, supplierUpdate} from "@/api/memberManage/vendorList";
import ProvinceCity from "@/views/components/provinceCity/index.vue";
import {listDept} from "@/api/system/dept";
import Info from "@/views/driverManage/driverList/components/info.vue";
import {isEmpty} from "lodash";


export default {
  name: "driverAdd",
  components: {Info, ProvinceCity},
  data() {
    return {
      loading: false,
      infoData: {
        driverBankAccounts: [],
      },
      state: 'write'
    };
  },
  watch: {
    // '$route': {
    //     handler(val) {
    //         console.log('this.$route', this.$route)
    //
    //         if (val && val.name == this.$options.name) {
    //             if (this.$route.query && this.$route.query.driverId && !this.$route.query.tabClick) {
    //                 this.getOldData(this.$route.query.driverId)
    //             } else if (!this.$route.query.tabClick) {
    //                 this.infoData = {}
    //             }
    //             this.state = this.$route?.query?.type
    //
    //             const title = this.$route?.query?.pageType ?? '司机'
    //             const route = Object.assign({}, this.$route, {title: `${title}`})
    //             this.$store.dispatch('tagsView/updateVisitedView', route)
    //         }
    //     },
    //     immediate: true,
    //     deep: true
    // }
  },
  mounted() {
    if (this.$route.query && this.$route.query.driverId) {
      this.getOldData(this.$route.query.driverId)
    }
    this.state = this.$route?.query?.type

    const title = this.$route?.query?.pageType ?? '司机'
    const route = Object.assign({}, this.$route, {title: `${title}`})
    this.$store.dispatch('tagsView/updateVisitedView', route)
  },
  methods: {
    cancel() {
      this.$router.back();
      this.$store.dispatch('tagsView/delView', this.$route);
    },
    getOldData(driverId) {
      this.loading = true;
      driverDetail(driverId).then(res => {
        this.infoData = res.data
        //this.infoData.driverBankAccounts = []
        // this.form = res.data
        // if (this.form.cityId && this.form.cityId != null) {
        //     //下标0为一级菜单value
        //     this.form.citydata[0] = this.form.provinceId;
        //     //下标1为二级菜单value
        //     this.form.citydata[1] = this.form.cityId;
        // }
      }).finally(() => {
        this.loading = false;
      });
    },
    async submitForm(type) {

      console.log('type', type)
      let that = this

      this.infoData.approvalStatus = type

      if (0 == type) {
        that.commit()
      } else if (1 == type) {
        let result = await this.$refs.form.valid();
        if (result) {
          that.flowCommit()

          // that.commit()
        }
      }

    },
    flowCommit() {
      let that = this
      this.loading = true;
      driverFlowAdd(that.infoData).then(res => {
        this.loading = false;
        this.$modal.msgSuccess("提交成功");
        this.$router.back();
        this.$store.dispatch('tagsView/delView', this.$route);
      }).finally(e => {
        this.loading = false;
      })
    },
    commit() {
      let that = this
      this.loading = true;

      if (that.infoData.driverId) {
        driverUpdate(that.infoData).then(res => {
          this.loading = false;
          this.$modal.msgSuccess("保存成功");
          this.$router.back();
          this.$store.dispatch('tagsView/delView', this.$route);
        }).finally(e => {
          this.loading = false;
        })
      } else {
        driverAdd(that.infoData).then(res => {
          this.loading = false;
          this.$modal.msgSuccess("保存成功");
          this.$router.back();
          this.$store.dispatch('tagsView/delView', this.$route);
        }).finally(e => {
          this.loading = false;
        })
      }
    },


  },


}
</script>


<style lang="scss" scoped>
::v-deep .el-form--label-top .el-form-item__label {
  padding: 0;
}

.el-form-item {
  margin-bottom: 15px;
  width: 70%;
}

::v-deep .el-date-editor--daterange {
  width: 100% !important;
}

::v-deep .el-select {
  width: 100%;
}

::v-deep .el-date-editor.el-input, .el-date-editor.el-input__inner {
  width: 100%;
}


</style>
